import { defineConfig, loadEnv } from "vite";
import vue2 from "@vitejs/plugin-vue2";
import path from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import legacy from "@vitejs/plugin-legacy";
import babel from "@rollup/plugin-babel";
// import copy from "rollup-plugin-copy";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), ""); // 取到对应mode的文件
  console.log("mode值", mode);
  return {
    base: mode === "fin" || mode === "finDev" ? "/cas/" : "/cms/", // 路由用
    plugins: [
      vue2(),
      createSvgIconsPlugin({
        // 指定需要缓存的svg图标文件夹即需要识别的svg都应该放在这个文件夹下
        // iconDirs: [path.resolve(process.cwd(), 'src/assets/svgs')],
        // 或
        iconDirs: [path.resolve(__dirname, "./src/assets/icons/svg")],
        // 指定symbolId格式这里的配置与6.2步骤中的引入svg组件的name配置项写法有关
        symbolId: "icon-[dir]-[name]",
      }),
      legacy({
        targets: ["defaults", "ie >= 10", "chrome 52"], //需要兼容的目标列表，可以设置多个
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
        modernPolyfills: true,
        renderLegacyChunks: true,
      }),
    ],
    resolve: {
      alias: [
        { find: "@", replacement: path.resolve(__dirname, "src") },
        {
          find: "vue",
          replacement: "vue/dist/vue.esm.js",
        },
      ],
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/assets/scss/style.scss";`,
        },
      },
    },
    server: {
      proxy: {
        // 代理配置
        "/caf": {
          target: "http://10.108.224.78:30006/",
          secure: false,
          rewrite: (path) => path.replace(/^\/caf/, ""),
          changeOrigin: true,
          // headers: {
          //   Referer: 'https://example.com'
          // }
        },
        "/mat": {
          target: "http://10.108.224.78:30007/",
          secure: false,
          rewrite: (path) => path.replace(/^\/mat/, ""),
          changeOrigin: true,
        },
        "/dict": {
          target: "http://10.108.224.78:30009/",
          secure: false,
          rewrite: (path) => path.replace(/^\/dict/, ""),
          changeOrigin: true,
        },
        "/trq": {
          target: "http://10.108.224.78:30011/",
          secure: false,
          rewrite: (path) => path.replace(/^\/trq/, ""),
          changeOrigin: true,
        },
        "/gate": {
          target: "http://10.108.224.78:30020/",
          secure: false,
          rewrite: (path) => path.replace(/^\/gate/, ""),
          changeOrigin: true,
        },
        "/ppp": {
          target: "https://mastest.changan.com.cn/am_rest",
          secure: false,
          rewrite: (path) => path.replace(/^\/ppp/, ""),
          changeOrigin: true,
        },
      },
    },
    build: {
      outDir: "cas", // 生成的文件夹名
    },
    rollupOptions: {},
  };
});
